package splash;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class SplashSprite {
	private Bitmap sprite;
	private int xPost;
	private int yPost;
	private Rect frame;
	private int timelifeFrame;
	private int numOFrame;
	private int currentFrame;
	private long time;
	private int frameHeight;
	private int frameWidth;
	private boolean loop;
	public boolean dispose;

	public SplashSprite() {
		this.xPost = 0;
		this.yPost = 0;
		this.time = 0;
		this.currentFrame = 0;
		this.dispose = false;
		this.frame = new Rect(0, 0, 0, 0);

	}

	public void Initialize(Bitmap bitmap, int frameHeight, int frameWidth,
			int fps, int numOFrame, boolean loop) {
		this.sprite = bitmap;
		this.frameHeight = frameHeight;
		this.frameWidth = frameWidth;
		this.frame.top = 0;
		this.frame.left = 0;
		this.frame.bottom = this.frameHeight;
		this.frame.right = this.frameWidth;
		this.timelifeFrame = 1000 / fps;
		this.loop = loop;
		this.numOFrame = numOFrame;
	}

	public int getxPost() {
		return xPost;
	}

	public int getyPost() {
		return yPost;
	}

	public void setxPost(int xPost) {
		this.xPost = xPost;
	}

	public void setyPost(int yPost) {
		this.yPost = yPost;
	}

	public void Update(Long gameTime) {
		if (gameTime >= this.time + timelifeFrame) {
			this.time = gameTime;
			currentFrame += 1;
			if (currentFrame >= numOFrame) {
				currentFrame = 0;
				if (!this.loop)
					this.dispose = true;
			}
		}
		this.frame.left = currentFrame * frameWidth;
		this.frame.right = this.frame.left + frameWidth;
	}

	public void draw(Canvas canvas) {
		Rect rec = new Rect(this.getxPost(), this.getyPost(), this.getxPost()
				+ this.frameWidth, this.getyPost() + this.frameHeight);
		canvas.drawBitmap(this.sprite, this.frame, rec, null);
	}
}
